home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13831 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: andrew.cmu.edu!sz24+
  2. From: "Stephan A. Zdancewic" <sz24+@andrew.cmu.edu>
  3. Newsgroups: comp.lang.misc,comp.lang.pl1,comp.lang.apl,comp.lang.c
  4. Subject: Re: GOTO controversy
  5. Date: Wed, 10 Apr 1996 10:18:02 -0400
  6. Organization: Senior, Math/Computer Science, Carnegie Mellon, Pittsburgh, PA
  7. Message-ID: <8lOwAOu00YUrQ7WHYl@andrew.cmu.edu>
  8. References: <314FB5F5.259B@simi.is> <3151B47F.70FD@connix.com>
  9.     <4jc1qa$lvv@news.microsoft.com>
  10. NNTP-Posting-Host: po8.andrew.cmu.edu
  11. In-Reply-To: <4jc1qa$lvv@news.microsoft.com>
  12.  
  13. a-cnadc@microsoft.com (Dann Corbit) writes:
  14. > In article <3151B47F.70FD@connix.com>, shawley@connix.com says...
  15. > >
  16. > >What to you think? Loops without using loops?
  17. > >
  18. > >a loop:
  19. > >
  20. > >for(i=0;i<10;i++)
  21. > >{
  22. > >  stuff
  23. > >}
  24. > >
  25. > >Looping with out a loop
  26. > >
  27. > >no_loop(0,10);
  28. > >
  29. > >no_loop(int start, int end)
  30. > >{
  31. > >    if(start != end) {
  32. > >        stuff
  33. > >        no_loop(start+1,end);
  34. > >    }
  35. > >}
  36. > >
  37. > >I haven't tested this but you get the idea.
  38. > Yes, recursion can be used to loop "without a loop".
  39. > There is no practical advantage to this method.
  40. > The recursive version has 10 function calls to add
  41. > to its overhead.
  42.  
  43. Actually, since this function is tail recursive, any good compiler
  44. will optimize the recursive function call to a jump to the start of
  45. the program block, effectively making this a loop.  Note that non-tail
  46. recursive functions will not be subject to this optimization, however.
  47.  
  48. Steve Zdancewic
  49. Carnegie Mellon University
  50.